package second.stackandqueue;

import java.util.LinkedList;
import java.util.List;

public class Solution04 {
    public boolean isValid(String s) {
        List<Character> stack = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                stack.add(')');
            } else if (s.charAt(i) == '[') {
                stack.add(']');
            } else if (s.charAt(i) == '{') {
                stack.add('}');
            } else if (stack.size() == 0 || s.charAt(i) != stack.get(stack.size() - 1)) {
                return false;
            } else {
                stack.remove(stack.size() - 1);
            }
        }
        return stack.size() == 0;
    }
}
